package com.mbschenkel.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.*;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class LatexProj1 implements EntryPoint {
	private MenuBar createMenuBar() {

		Command cmd = new Command() {
			public void execute() {
				Window.alert("You selected a menu item!");
			}
		};

		/* Menu Bar */
		// Make some sub-menus that we will cascade from the top menu.
		MenuBar newMenu = new MenuBar(true);
		newMenu.addItem("Project", cmd);
		newMenu.addItem("File", cmd);
		newMenu.addItem("Block", cmd);

		MenuBar fileMenu = new MenuBar(true);
		fileMenu.addItem("New", newMenu);
		fileMenu.addItem("Open", cmd);
		fileMenu.addItem("Save", cmd);
		fileMenu.addItem("Export PDF", cmd);
		fileMenu.addItem("Print", cmd);
		fileMenu.addSeparator();
		fileMenu.addItem("Close", cmd);

		MenuBar editMenu = new MenuBar(true);
		editMenu.addItem("Copy", cmd);
		editMenu.addItem("Cut", cmd);
		editMenu.addItem("Paste", cmd);

		MenuBar projectMenu = new MenuBar(true);
		projectMenu.addItem("Add collaborator", cmd);
		projectMenu.addItem("History", cmd);
		projectMenu.addItem("Help", cmd);

		// Make a new menu bar, adding a few cascading menus to it.
		MenuBar menu = new MenuBar();
		menu.addItem("File", fileMenu);
		menu.addItem("Edit", editMenu);
		menu.addItem("Project", projectMenu);

		return menu;
	}

	LatexSnippetManager snippetManager; 


	private String createSidePanelTitle(String title, String img) {
		HTML titleHTML = new HTML( title );
		if( img != "")
			titleHTML.setHTML(Images.buildHTML(img)+" "+title);
		titleHTML.setStyleName("mbsStackPanelHeader");
		GWT.log(titleHTML.getHTML(),null);
		return titleHTML.getHTML();
	}
	private Panel createSidePanel() {
		//DecoratedStackPanel sidePanel = new DecoratedStackPanel();
		StackPanel sidePanel = new StackPanel();
		sidePanel.setWidth("200px");
		sidePanel.setHeight("100%");
		sidePanel.add(createTaskItems(), createSidePanelTitle("Tasks", Images.getTasks() ), true);
		sidePanel.add(new Label("Bar"), createSidePanelTitle("Content-Tree", Images.getFolder() ), true);

		snippetManager = new LatexSnippetManager();
		sidePanel.add( snippetManager, createSidePanelTitle("LaTeX Symbols",Images.getSnippet()), true);
		
		return sidePanel;
	}

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		// ev. have everything grid based?
		DockPanel mainDockPanel = new DockPanel();
		mainDockPanel.setWidth("100%");
		mainDockPanel.setHeight("100%");
		mainDockPanel.setSpacing(5);
		// mainDockPanel.setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		RootPanel.get().add(mainDockPanel);

		// Add it to the root panel.
		mainDockPanel.add(createMenuBar(), DockPanel.NORTH);

		// Create a stack panel containing three labels.
		Panel sidePanel = createSidePanel();
		mainDockPanel.add(sidePanel, DockPanel.WEST);
		mainDockPanel.setCellHeight(sidePanel, "100%");

		/* other elements */
		// Image img = new
		// Image("http://code.google.com/webtoolkit/logo-185x175.png");
		// Button button = new Button("Click me");
		// We can add style names
		// button.addStyleName("pc-template-btn");
		// or we can set an id on a specific element for styling
		// img.getElement().setId("pc-template-img");
		TabPanel fileTabPanel = new TabPanel();
		// int clWidth = Window.getClientWidth() - 250;
		// fileTabPanel.setWidth( clWidth + "px"); // TODO should just be 100%,
		// this is not ideal...
		fileTabPanel.setSize("100%","100%");
		fileTabPanel.add(Session.getInstance().getSessionPanel(),Images.buildHTML(Images.getSession()) + " Session", true);
		fileTabPanel.add(new FileTab(), Images.buildHTML(Images.getDocument()) + " Tabby", true);
		fileTabPanel.add(new FileTab(), Images.buildHTML(Images.getDocument()) + " Blub", true);
		fileTabPanel.add(new AboutTab(), Images.buildHTML(Images.getUser()) + " About", true);
		fileTabPanel.selectTab(0);
		mainDockPanel.add(fileTabPanel, DockPanel.NORTH);
		mainDockPanel.setCellWidth(fileTabPanel, "100%");

		// Create the dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Welcome to GWT!");
		dialogBox.setAnimationEnabled(true);
		Button closeButton = new Button("close");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.setWidth("100%");
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		dialogVPanel.add(closeButton);

		closeButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				dialogBox.hide();
			}
		});

		// Set the contents of the Widget
		dialogBox.setWidget(dialogVPanel);

		/*
		 * button.addClickListener(new ClickListener() { public void
		 * onClick(Widget sender) { dialogBox.center(); dialogBox.show();
		 * makeCall(Window.prompt("message?", "f")); } });
		 */
	}

	/**
	 * Create the list of Tasks to the left.
	 * 
	 * @return the list of contacts
	 */
	private VerticalPanel createTaskItems() {
		// Create the list of tasks
		VerticalPanel contactsPanel = new VerticalPanel();
		contactsPanel.setSpacing(4);
		String[] contactNames = { "ab", "cd" };
		for (int i = 0; i < contactNames.length; i++) {
			final String contactName = contactNames[i];
			final HTML contactLink = new HTML(
					"<a href=\"javascript:undefined;\">" + contactName + "</a>");
			contactsPanel.add(contactLink);
		}
		return contactsPanel;
	}

}
